package com.chujian.Activities;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.chujian.Adapter.OrderCurrentDetailSellerAdapter;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.Utils.Util;
import com.chujian.model.FoodList;
import com.chujian.model.Order;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class OrderCurrentDetailSellerActivity extends Activity implements OnItemLongClickListener{
	private TextView mTvOrderId;
	private TextView mTvTableName;
	private Button mBtnCancelConfirm;
	private ListView mListView;
	private List<FoodList> mList;
	private int position;
	private Order mOrder;
	private OrderCurrentDetailSellerAdapter mAdapter;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(str.equals("checkOutSuccess")){
				Util.confirmDialog("提示", "结算成功", OrderCurrentDetailSellerActivity.this);
				Gson gson =new Gson();
            	String jsonString=gson.toJson(mOrder);
            	Intent intent =new Intent(getApplicationContext(), OrderHistoryDetailSellerActivity.class);
            	intent.putExtra("historyOrder", jsonString);
            	if(position!=-1){
            		
            	Intent intent2=new Intent();
            	intent2.putExtra("position", position);
            	setResult(1001, intent2);
            	}
            	Button btn=(Button) findViewById(R.id.btn_ods_bottom_left);
            	btn.setClickable(false);
            	btn.setBackgroundResource(R.drawable.gray_button_selector);
            	startActivity(intent);
            	finish();
			}else if(str.equals("checkOutFailed")){
				Util.confirmDialog("提示", "结算失败", OrderCurrentDetailSellerActivity.this);
				
			}else if(str.equals("cancelSuccess")){
				Util.confirmDialog("提示", "订单取消成功！！", OrderCurrentDetailSellerActivity.this);
				mBtnCancelConfirm.setClickable(false);
				
			}else if(str.equals("cancelFailed")){
				Util.confirmDialog("提示", "订单取消失败！！", OrderCurrentDetailSellerActivity.this);
				
			}else if(str!=null){
				mList=JsonUtil.FoodListFromJson(str);
				mAdapter=new OrderCurrentDetailSellerAdapter(mList, OrderCurrentDetailSellerActivity.this);
				mListView.setAdapter(mAdapter);
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_current_detail_seller);
		initView();
		if(mOrder!=null){
			String url=Constan.BASE_URL+"OrderCurrentDetailSellerFoodList";
			new HttpClientPostThread(url, mHandler, mOrder.getOrderId()).start();
		}
				
		
	}
	private void initView() {
		// TODO Auto-generated method stub
		mTvOrderId=(TextView) findViewById(R.id.tv_order_current_detail_seller_id);
		mTvTableName=(TextView) findViewById(R.id.tv_order_current_detail_seller_table);
		mListView=(ListView) findViewById(R.id.lv_order_detail_seller);
		mListView.setOnItemLongClickListener(this);
		mBtnCancelConfirm=(Button) findViewById(R.id.btn_ods_bottom_right);
		Intent intent=getIntent();
		mOrder =(Order) intent.getSerializableExtra("currentOrder");
		position=intent.getIntExtra("position", -1);
		if(mOrder==null){
			String jsondata=intent.getStringExtra("orderNotice");
			mOrder=JsonUtil.OrderFromJson(jsondata);
		}
		if(mOrder!=null){
			mTvOrderId.setText(mOrder.getOrderId());
			mTvTableName.setText(mOrder.getTableName());
		}
		if(mOrder.getCancelFlag()==1){
			mBtnCancelConfirm.setVisibility(View.VISIBLE);
		}else{
			mBtnCancelConfirm.setVisibility(View.GONE);
		}
		
	}
	public void IbBack(View v){
		OrderCurrentDetailSellerActivity.this.finish();
	}
	public void doClick(View v){
		switch (v.getId()) {
		case R.id.btn_ods_bottom_left:
//			订单结算
			confirmDialog();
			break;
			
		case R.id.btn_ods_bottom_right:
			cancelConfirmDialog();
			break;
		default:
			break;
		}
	}
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		return false;
	}
	
	private void confirmDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(OrderCurrentDetailSellerActivity.this);
        builder.setTitle("应收金额："+mOrder.getTotalPrice()+"元");
        builder.setMessage("请确认您已经向"+mOrder.getTableName()+"用户"+mOrder.getUserPhone()+"收取足够金额！！");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	Util.showTextLong(getApplicationContext(), "正在进行订单结算，请稍候....");
				String url = Constan.BASE_URL + "OrderCheckOutServlet";
				Gson gson = new Gson();
				String jsonString = gson.toJson(mOrder);
				new HttpClientPostThread(url, mHandler, jsonString).start();
				;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            }
        });
        builder.show();
	}
	
	
	private void cancelConfirmDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(OrderCurrentDetailSellerActivity.this);
        builder.setTitle("警告！");
        builder.setMessage("确认取消订单吗？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	Util.showTextLong(getApplicationContext(), "正在取消订单，请稍候....");
				String url = Constan.BASE_URL + "OrderCurrentCancelConfirm";
				Gson gson = new Gson();
//				String jsonString = gson.toJson(mOrder);
				new HttpClientPostThread(url, mHandler, mOrder.getOrderId()).start();
				;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            }
        });
        builder.show();
	}
}
